package com.example.demo.yuy.demo.controller;

import com.example.demo.aspect.WebLog;
import com.example.demo.config.ParaHelper;
import com.example.demo.config.ResponseHelper;
import com.example.demo.config.ResponseModel;
import com.example.demo.handler.BusinessException;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TestMapController {

    private static Map<String, String> MAP = new HashMap<>();

    static {
        MAP.put("key1", "value1");
        MAP.put("key2", "value2");
        MAP.put("key3", "value3");
        MAP.put("key4", "value4");
    }

    @GetMapping(path = "/getValue1")
    @WebLog(description = "获取key1的值")
    @ApiOperation(value = "添加任务",notes = "添加任务",consumes = "application/json")
    public void getValue1() {
        if (null == MAP.get("key1")) {
            MAP.put("key1", "value1");
        }

        System.out.println("value1");
    }

    @GetMapping(path = "/getValue2/{key2}")
    @WebLog(description = "获取key2的值")
    public String getValue2(@PathVariable("key2") String key2) {
        return MAP.get(key2);
    }

    @GetMapping(path = "/getValue4")
    @WebLog(description = "获取key4的值")
    public ResponseModel<String> getValue4(@RequestParam(name = "key4") String key4,
                                      @RequestParam(name = "test") String test) {

        if (StringUtils.isEmpty(test)) {
            throw new BusinessException("测试数据不能为空");
        }
        return ResponseHelper.success(MAP.get(key4)+"yuayng","");
    }

    @GetMapping(path = "/getValue3")
    public String getValue3() {
        return ParaHelper.getPath("userName");
    }

    @RequestMapping(value = "/testVo", method = RequestMethod.POST)
    @WebLog(description = "获取用户id")
    public ResponseModel<String> testVolai(@Validated @RequestBody User user) {
//        Assert.notNull(user.getUserId(), "用户id不能为空");

        return ResponseHelper.success(user.getUserId(), "");
    }

    @RequestMapping(value = "/testVo3", method = RequestMethod.POST)
    @WebLog
    public ResponseModel<String> testVolai3(@RequestBody String userId) {
//        Assert.notNull(userId, "用户id不能为空");

        if (StringUtils.isEmpty(userId)) {
            throw new BusinessException("用户id不能为空");
        }
        return ResponseHelper.success(userId, "");
    }

    @RequestMapping(value = "/testVo2/{userId}", method = RequestMethod.GET)
    public String testVolai2(
        @PathVariable @Valid @NotEmpty(message = "用户id不能为空") String userId) {
        Assert.notNull(userId, "用户id不能为空");

        return userId;
    }
}
